package com.yanqu.road.dao.impl.channelcenter.washe;

import com.yanqu.road.dao.cache.SqlCacheParam;
import com.yanqu.road.dao.impl.TempNoPoolCacheDao;
import com.yanqu.road.entity.activity.washe.config.WasheCardConfig;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TempWasheCardConfigDaoImpl extends TempNoPoolCacheDao {

    public TempWasheCardConfigDaoImpl(String dbParam) {
        super(dbParam);
    }

    private static String getWasheCardConfigSql = "select * from `t_n_washepvp_card` where `activityId`=?";

    public Map<Integer, WasheCardConfig> getOperaMaskRewardConfigMap(int activityId) {

        SqlCacheParam sqlCacheParam = getCacheParam(getWasheCardConfigSql);
        sqlCacheParam.addParam(Integer.class, activityId);
        // 先取缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer, WasheCardConfig>) cacheResult;
        }

        Map<Integer, WasheCardConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                WasheCardConfig config = new WasheCardConfig();
                config.setActivityId(rs.getInt("activityId"));
                config.setCardId(rs.getInt("cardId"));
                config.setQuality(rs.getInt("quality"));
                config.setPrice(rs.getInt("price"));
                config.setSkillParam(rs.getString("skillParam"));
                config.setCd(rs.getInt("cd"));
                config.setPic(rs.getString("pic"));
                config.setCardName(rs.getString("cardName"));
                config.setCardDesc(rs.getString("cardDesc"));
                config.setItemId(rs.getInt("itemId"));
                config.setMaxTime(rs.getInt("maxTime") * DateHelper.SECOND_MILLIONS);

                // init
                config.setSkillParamList(StringUtils.stringToLongList(config.getSkillParam(), "\\|"));

                map.put(config.getCardId(), config);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        // 添加缓存
        addCacheResult(sqlCacheParam, map);
        return map;
    }

}
